package app.server.s02;

import java.util.ArrayList;
import java.util.List;

import app.model.AppBasedoc;
import app.model.AppClient;

import com.agile.jfinal.controller.CoreController;
import com.agile.jfinal.utils.DataUtils;
import com.jfinal.ext.render.DwzRender;
import com.lasun.server.MessageServer;

/**
 * APP客户端信息查询
 * @author qiutian  2014年3月24日
 */
public class AppClientController extends CoreController {

	
	/**
	 * 默认方法查询数据
	 */
	public void index(){

		//获取两个查询条件
		AppClient appClient = getModel(AppClient.class);
		setAttr("appClient", appClient);
		
		AppBasedoc appBasedoc = getModel(AppBasedoc.class); 
		setAttr("appBasedoc", appBasedoc);
		
		//查询是否在线
		Boolean is_online = appClient.getBooleanByInt(appClient.IS_ONLINE);
		String sqlExceptSelect = "";
		if(is_online!=null && is_online==true){
			//查询APP当前在线客户端
			List<String[]> listOnlineClient = MessageServer.onlineClientCount(appBasedoc.queryById().getStr(appBasedoc.VCODE));
			
			if(listOnlineClient.size()>0){
				List<String> listVode = new ArrayList<String>();
				for (String[] strings : listOnlineClient) {
					listVode.add(strings[9].split(":/")[1]);
				}
				
				sqlExceptSelect = " from app_basedoc a, app_client b where a.pk_basedoc=b.pk_basedoc " +
						"  and "+DataUtils.createWhereSQL("b."+AppClient.VCODE,listVode);
				setAttr("page",appClient.paginate(getPageNumber(), getPageSize(), " select * ", sqlExceptSelect)); //分页查询
			}else{
				
			}
		}else{
			//查询APP全部客户端
			sqlExceptSelect = " from app_basedoc a, app_client b where a.pk_basedoc=b.pk_basedoc " +
					"  and a.pk_basedoc='"+appBasedoc.getPrimaryValue()+"'";
			setAttr("page",appClient.paginate(getPageNumber(), getPageSize(), " select * ", sqlExceptSelect)); //分页查询
		}
		
		renderJsp("/WEB-INF/jsp/app/server/s02/app_client_list.jsp");
		
	}

	/**
	 * 保存数据
	 */
	public void saveModel(){
		if(getModel(AppClient.class).save()){
			
			DwzRender dwz = new DwzRender();
			dwz.message("操作成功");
			dwz.callbackType("closeCurrent");
			dwz.navTabId("app_client_list");
			render(dwz);
		}else{
			render(DwzRender.error());
		}
		
	}
	
	/**
	 * 转入新增页面
	 */
	public void addPage(){
		renderJsp("/WEB-INF/jsp/app/server/s02/app_client_form.jsp");
	}
	
	/**
	 * 转入修改页面
	 */
	public void editPage(){
		setAttr("AppClient", getModel(AppClient.class).queryById());
		renderJsp("/WEB-INF/jsp/app/server/s02/app_client_form.jsp");
	}
	
	/**
	 * 删除数据
	 */
	public void delModel(){
		getModel(AppClient.class).delete();
		render(DwzRender.success());
	}
	
	/**
	 * 转入修改页面
	 */
	public void detailPage(){
		setAttr("AppClient", getModel(AppClient.class).queryById());
		renderJsp("/WEB-INF/jsp/app/server/s02/app_client_detail.jsp");
	}
}
